﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using mvcDemo.Models;

namespace mvcDemo.Controllers
{
    public class resourceController : ApiController
    {
        
        // GET api/resource/5
        /// <summary>
        /// 获取并显示指定图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public HttpResponseMessage Get(int id)
        {
            using (Entities db = new Entities())
            {
                PICDATA picData = db.PICDATA.Find(id);
                byte[] img = new byte[0];
                if (picData != null && !picData.FILEDATA.Equals(""))
                {
                    img = (byte[])picData.FILEDATA;
                }

                var resp = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new ByteArrayContent(img)
                };
                resp.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpg");
                return resp;
            }
        }
    }
}
